<?php
namespace app\app\controller\v1;
use think\Db;
use app\app\util\OnlineUtils;

class BargainController extends  BaseController{


    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 砍价线路（薄荷玩乐、长滩玩乐）
     */
    public function BargainLineList()
    {
        $pageindex = input('post.pageindex');
        $pagesize = input('post.pagesize');
        if( $pageindex <= 0 ){
            $length = 0;
        }else{
            $length = $pagesize*$pageindex;
        }
        $data = Db::name('line')->alias('l')
            ->join('line_class lc','l.class_id = lc.id','left')
            ->where(['l.status'=>1,'l.ware_type_id'=>4])
            ->order('l.sort desc')
            ->field('l.id,l.cover,l.title,l.price,l.ware_type_id,l.sort,lc.line_class_name,l.lower_price')
            ->limit($length,$pagesize)
            ->select();
        return json_code(200,'success',$data);
    }


    /**
     * 用户创建砍价()
     */
    public function createBargain()
    {
        //用户id
        $user_id = OnlineUtils::getUserIdBySid();
        $line_id = input('get.line_id');
        $line = Db::name('line')->field('id,cover,title,price,lower_price,ware_type_id')->find($line_id);
        //判断
        if( $line['ware_type_id']  !== 4 ){
            return json_code(402,'该线路不参与砍价活动');
        }
        //自己所砍所砍金额
        $arr = rand(50,99);
        //该线路已经砍价金额
        $al_money = $arr + 0;
        //现在价格
        $now_price = $line['price']-$arr;
        //达到最低价格还差金额数
        $dis_price = $now_price-$line['lower_price'];
        //当前时间
        $time = time();
        //生成砍价（先判断用户是否存在正在进行的砍价商品）
        $res = Db::name('bargain')->where(['user_id'=>$user_id,'bargain_status'=>1])->find();
        if( $res ){
            return json_code(402,'您有正在砍价的线路');
        }else{
            $data = array('user_id'=>$user_id,'line_id'=>$line_id,'create_time'=>$time);
            $bargainId = Db::name('bargain')->insertGetId($data);
            //砍价记录
            if( $bargainId ){
                $d = array('bargain_id'=>$bargainId,'uid'=>$user_id,'create_time'=>$time,'money'=>$arr,
                    'al_money'=>$al_money,'now_price'=>$now_price,'dis_price'=>$dis_price);
                Db::name('bargain_list')->insertGetId($d);
            }
        }

        return json_code(200,'success',
            ['line'=>$line,'money'=>$arr,'bargainId'=>$bargainId,'now_price'=>$now_price,'dis_price'=>$dis_price]);



    }


    /**
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * 帮助好友砍价
     */
    public function helpBargain()
    {
        //帮砍价用户id
        $user_id = OnlineUtils::getUserIdBySid();
        //砍价id
        $bargainId = input('get.bargain_id');
        //砍价信息
        $bargian = Db::name('bargain')->where('bargain_status = 1')->find($bargainId);
        if( !$bargian){
            return json_code(402,'不存在该砍价活动');
        }
        //当前时间
        $time = time();
        //最后一次帮砍记录
        $last_bargain = Db::name('bargain_list')->where(['bargain_id'=>$bargainId])->order('create_time desc')->find();
        //判断当前时间距离砍价开始的时间是否超过24h,最后一次帮砍记录中距离达到最低价还差多少
        $dis_time = $time-$bargian['create_time'];
        if( $dis_time >= 86400 && $last_bargain['dis_price'] >= 0 ){
            Db::name('bargain')->update(array('bargain_id'=>$bargainId,'status'=>2));
            return json_code(402,'该砍价商品已经结束砍价，您的好友未能砍到最低价');
        }elseif ($dis_time < 86400 && $last_bargain['dis_price'] < 0){
            return json_code(200,'该砍价商品已经结束砍价,您的好友成功砍到最低价');
        }elseif ( $dis_time < 86400 && $last_bargain['dis_price'] >0){
            //可以帮好友砍价
            //距离砍价成功的金额>=2000，砍价金额为50-90，2000>距离砍价成功的金额>=1000，砍价金额为40-49，
            //1000>距离砍价成功的金额>=500，砍价金额为30-39.....
            if($last_bargain['dis_price'] >=2000){
                $rand = rand(50,90);
            }elseif ( $last_bargain['dis_price'] < 2000 && $last_bargain['dis_price'] >=1000 ){
                $rand = rand(40,49);
            }elseif ( $last_bargain['dis_price'] < 1000 && $last_bargain['dis_price'] >=500 ){
                $rand = rand(30,39);
            }elseif (  $last_bargain['dis_price'] < 500 && $last_bargain['dis_price'] >=300 ){
                $rand = rand(20,29);
            }elseif (  $last_bargain['dis_price'] < 500 && $last_bargain['dis_price'] >=300 ){
                $rand = rand(10,19);
            }elseif (  $last_bargain['dis_price'] < 300 && $last_bargain['dis_price'] >=100 ){
                $rand = rand(5,9);
            }elseif (  $last_bargain['dis_price'] < 100 && $last_bargain['dis_price'] >=1 ){
                $rand = rand(1,4);
            }
            //已经砍了金额数
            $al_money = $last_bargain['al_money'] + $rand;
            //现在的已经砍到金额数
            $now_price = $last_bargain['now_price'] - $rand;
            //还差金额数
            $dis_price = $last_bargain['dis_price'] - $rand;
            $data = array('bargain_id'=>$bargainId,'uid'=>$user_id,'create_time'=>time(),'money'=>$rand,
                'al_money'=>$al_money,'now_price'=>$now_price,'dis_price'=>$dis_price);
            $help_bargain = Db::name('bargain_list')->insertGetId($data);
            //用户帮砍记录
            $res =Db::name('bargain_list')->find($help_bargain);
            //所有用户帮砍列表
            $help_list = Db::name('bargain_list')->alias('b')
                ->join('user u','b.uid = u.id','left')
                ->field('b.*,u.nickname,u.avatar_url')
                ->where(['b.bargain_id'=>$bargainId])->select();
            if( !$help_bargain){
                return json_code(402,'帮砍失败');
            }
            return json_code(200,'success',['res'=>$res,'help_list'=>$help_list]);
        }

    }



}